package com.jelly.web.share.userInfo;

import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.constant.MessageConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.config.webSocket.WebSocket;
import com.jelly.web.modules.message.domain.Message;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.modules.staff.user.domain.User;
import com.jelly.web.modules.staff.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;


/**
 * @创建人 ql
 * @创建时间 2018-10-12 10:31
 * @项目名称 jelly
 * @功能描述: 用户信息控制器
 **/
@Controller
@RequestMapping("/share/user-infor")
public class UserInforController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 个人资料跳转
     * @return
     */
    @GetMapping
    public String getUserInfor(Model model){
        //获取字典详情
        getDictionary();

        model.addAttribute("user",TokenUtil.getToken());

        return adminUserInfor("/user");

    }

    /**
     * 修改密码跳转
     * @return
     */
    @GetMapping("/password")
    public String getPassword(){

        return adminUserInfor("/password");

    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/password")
    @OperationLog(name = "个人模块-密码",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updatePassword(User user, String newPassword){
        user.setUserId(TokenUtil.getUserId());
        switch (userService.updatePassword(user,newPassword)){
            case 0:
                Message message = new Message();
                message.setTargetId("1");
                message.setAction("修改密码操作");
                message.setMsgType(MessageConstant.SYSTEM);
                WebSocket.sendMessage(message);
                return okResponse("修改成功");
            case 1:
                return errorResponse("修改失败");
            default:
                return errorResponse("原密码错误");
        }

    }


    /**
     * 个人资料修改
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "个人模块-信息",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateUserInfor(User user){
        user.setUserId(TokenUtil.getUserId());

        if(StringUtils.isNotBlank(user.getNickname())){
            Set<String> set = verificationSensitiveWord(user.getNickname());
            if(ListUtil.isNotSet(set)){
                return errorResponse("昵称包含严禁词:"+set.toString());
            }
        }

        return userService.updateUser(user)?okResponse("修改成功"):errorResponse("修改失败");

    }


    /**
     * 头像裁剪跳转
     * @param model
     * @return
     */
    @GetMapping("/user-upload")
    public String userImg(Model model){
        model.addAttribute("user",TokenUtil.getToken());
        return adminUserInfor("/userupload");
    }


    /**
     * 修改头像
     * @param file
     * @return
     */
    @PostMapping("/image")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> image(@RequestParam("file") MultipartFile file){

        return userService.updatUserImg(file)?okResponse("修改成功"):errorResponse("修改失败请联系管理员");

    }

}
